﻿#pragma execution_character_set("utf-8")
#include "form.h"
#include "ui_form.h"
#include <QMessageBox>
#include <QtCharts>

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#  include <QDesktopWidget>
#endif
#include <QScreen>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint);//无边框  
    m_areaMovable = geometry();
    m_bPressed = false;

    customPlot = ui->chart_3; // 创建一个指针，方便使用，省去  "ui->"
    customPlot->addGraph();   //添加一条线

    customPlot->xAxis->setRange(MinX, MaxX);   //这里我们设置默认的轴值
    customPlot->yAxis->setRange(MinY, MaxY);

    //可以进行鼠标位置 放大缩小 拖拽  放大缩小坐标系！！！功能非常强大
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);


}

Form::~Form()
{
    delete ui;
}

void Form::on_pushButton_clicked()
{
    QSplineSeries* series = new QSplineSeries();
    series->setName("spline");
    /*series->append(0, 6);
    series->append(2, 4);
    series->append(3, 8);
    series->append(7, 4);
    series->append(10, 5);*/
    int i = 0;
    for ( i = 0; i < 1000; i++)
    {
        series->append(i, i);
    }

    m_chart = new QChart();
    m_chart->legend()->hide();
    m_chart->addSeries(series);
    m_chart->setTitle("Simple spline chart example");
    m_chart->createDefaultAxes();
    m_chart->axisY()->setRange(0, i+2);

    QChartView* chartView = new QChartView(m_chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    QGridLayout* baseLayout = new QGridLayout();
    baseLayout->addWidget(chartView);
    ui->chart->setLayout(baseLayout);

}



void Form::on_pushButton_2_clicked()
{
    //曲线重绘
        ui->customPlot->replot();
    //添加两幅图并填充内容
        //第一幅图
        ui->customPlot->addGraph();
        //线的颜色
        ui->customPlot->graph(0)->setPen(QPen(Qt::blue));
        //用半透明的蓝色填充
        ui->customPlot->graph(0)->setBrush(QBrush(QColor(0,0,255,20)));

        //第二幅图
        ui->customPlot->addGraph();
        ui->customPlot->graph(1)->setPen(QPen(Qt::red));

        //生成数据点
        QVector<double> x(251),y0(251),y1(251);
        for(int i=0;i<251;i++){
            x[i]=i;
            y0[i]=qExp(-i/150.0)*qCos(i/10.0);//指数衰减的余弦曲线
            y1[i]=qExp(-i/150.0);//指数包裹曲线
        }
        //配置上和右坐标轴来显式刻度,但是不显示数字
        ui->customPlot->xAxis2->setVisible(true);
        ui->customPlot->xAxis2->setTickLabels(false);
        ui->customPlot->yAxis2->setVisible(true);
        ui->customPlot->yAxis->setTickLabels(false);

        //修改左和底坐标轴,使之与右和上坐标轴始终匹配
        connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
        connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));

        //为每幅图设置点
        ui->customPlot->graph(0)->setData(x,y0);
        ui->customPlot->graph(1)->setData(x,y1);

        //使图0自适应范围
        ui->customPlot->graph(0)->rescaleAxes();
        //图1页一样,但是只允许放大,以免比图0小
        ui->customPlot->graph(1)->rescaleAxes(true);
        //注意,以上两步也可以用ui->customPlot->rescaleAxes();来代替

        //允许用户用鼠标拖拉、缩放、选择任一幅图
        ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}


double assignValue(double &X, double &Y)
{
    double xNumber = X;
    double yNumber = -4096+qrand() % 8192;

    X = xNumber;
    Y = yNumber;

    return X, Y;
}

void Form::on_pushButton_3_clicked()
{
    double xNumber = x.count();
    double yNumber = 0;
    assignValue(xNumber, yNumber);

    if(xNumber > MaxX)
        {
            MaxX = xNumber;
        }
    if(xNumber < MinX)
        {
            MinX = xNumber;
        }
    if(yNumber > MaxY)
        {
            MaxY = yNumber;
        }
    if(yNumber < MinY)
        {
            MinY = yNumber;
        }
    x.push_back(xNumber);
    y.push_back(yNumber);


    if (MaxX - 20 > MinX) 
    {
        customPlot->xAxis->setRange(MinX++, MaxX);//这里我们设置默认的轴值
    }

    customPlot->yAxis->setRange(MinY,MaxY);
    customPlot->graph(0)->setData(x,y);
    customPlot->replot();
}

void Form::mousePressEvent(QMouseEvent* e)
{
    //鼠标左键
    if (e->button() == Qt::LeftButton)
    {
        m_ptPress = e->pos();
        //qDebug() << pos() << e->pos() << m_ptPress;
        m_bPressed = m_areaMovable.contains(m_ptPress);
    }
}

void Form::mouseMoveEvent(QMouseEvent* e)
{
    if (m_bPressed)
    {
        //qDebug() << pos() << e->pos() << m_ptPress;
        move(pos() + e->pos() - m_ptPress);
    }
}

void Form::mouseReleaseEvent(QMouseEvent*)
{
    m_bPressed = false;
}

//设置鼠标按下的区域
void Form::setAreaMovable(const QRect rt)
{
    if (m_areaMovable != rt)
    {
        m_areaMovable = rt;
    }
}
